<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { Delete, ZoomIn,Plus, Download, Management,Loading } from '@element-plus/icons-vue'
import { updateAvatar } from '@/api/user'
const imageUrl = ref('')
const dialogImageUrl = ref('')
const dialogVisible = ref(false)
const loading = ref(false)
const uploadRef = ref(false)
const handleAvatarSuccess = (response, uploadFile) => {
   if (response.data.code === '200') {
        imageUrl.value = response.data.data
        ElMessage.success('修改成功') 
        loading.value = false
   }
}

const  beforeAvatarUpload=(rawFile)=> {
  if (!['image/jpeg', 'image/png'].includes(rawFile.type)) {
    ElMessage.error('仅支持 JPG/PNG 格式')
    loading.value=true
    return false
  }
}

//上传
const handlePictureCardPreview = (file) => {
  console.log(file);

};


//下载
const handleDownload = (file) => {
  console.log(file.response.data);
  dialogImageUrl.value = file.response.data;
  dialogVisible.value = true;
};

//清空路径
const clearFiles = (file) => {
  file.url = null;
};


// const cancal = async() => {
//   imageUrl.value = ''
// }





</script>

<template>

<el-card  class="box-card">
    <h2>修改头像</h2>
        <el-form-item label="上传:" style="margin-left: 30px;">
          <el-upload action="http://localhost:8888/upload-img" list-type="picture-card" :auto-upload="true">
            <el-icon>
              <Plus />
            </el-icon>

            <template #file="{ file }">
              <div>
                <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
                <span class="el-upload-list__item-actions">
                  <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
                    <el-icon>
                      <ZoomIn />
                    </el-icon>
                  </span>
                  <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleDownload(file)">
                    <el-icon>
                      <Download />
                    </el-icon>
                  </span>
                  <span v-if="!disabled" class="el-upload-list__item-delete" @click="clearFiles(file)">
                    <el-icon>
                      <Delete />
                    </el-icon>
                  </span>
                </span>
              </div>
            </template>
          </el-upload>

          <el-dialog v-model="dialogVisible">
            <img w-full :src="dialogImageUrl" alt="Preview Image" />
          </el-dialog>
        </el-form-item>

            <!-- <el-button type="primary" @click="submitForm()">提交</el-button>
            <el-button @click="cancal()">取消</el-button>
        -->
    </el-card>
</template>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>